博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Webform Repeater的灵活运用
阅读量:5843 次
发布时间:2019-06-18

本文共 5742 字,大约阅读时间需要 19 分钟。

案例:模拟购物列表

封装实体类:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 ///  7 /// gouwu 的摘要说明 8 ///  9 public class gouwu10 {11     public int ids { get; set; }12     public string pic { get; set; }13     public string name { get; set; }14     public decimal nowPrice { get; set; }15     public decimal oldPrice { get; set; }16     public string context { get; set; }17 18     public string qx {19         get20         {21             string eee = "";22             if (this.name == "猕猴桃")23             {24                 eee = " display:none;";25             }26 27             return eee;28         }29     }30 31 }
View Code

数据访问类:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.SqlClient; 6  7 ///  8 /// gouwuData 的摘要说明 9 /// 10 public class gouwuData11 {12     SqlConnection conn = null;13     SqlCommand cmd = null;14     public gouwuData()15     {16         conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");17         cmd = conn.CreateCommand();18     }19 20     public List
Select()21 {22 List
glist = new List
();23 cmd.CommandText = "select *from gouwu";24 25 conn.Open();26 SqlDataReader dr = cmd.ExecuteReader();27 if (dr.HasRows)28 {29 while (dr.Read())30 {31 gouwu g = new gouwu();32 g.ids = Convert.ToInt32(dr[0]);33 g.pic = dr[1].ToString();34 g.name = dr[2].ToString();35 g.nowPrice = Convert.ToDecimal(dr[3]);36 g.oldPrice = Convert.ToDecimal(dr[4]);37 g.context = dr[5].ToString();38 39 glist.Add(g);40 41 }42 }43 44 45 conn.Close();46 47 return glist;48 }49 50 51 public void Delete(int ids)52 {53 cmd.CommandText = "delete from gouwu where ids='" + ids + "'";54 55 conn.Open();56 cmd.ExecuteNonQuery();57 conn.Close();58 }59 60 61 }
View Code

用Repeater展示:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  2   3   4   5   6   7     
8 9 83 84 85 86
87
88
89
90
91
92
" /> 93
<%#Eval("name") %>
94
价格:<%#Eval("nowPrice") %>
<%#Eval("oldPrice") %>
95
<%#Eval("context") %>
96
97
98
99
100
101
102 103
104
105 106
界面:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class _Default : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         if (!IsPostBack)13         {14             Repeater1.DataSource = new gouwuData().Select();15             Repeater1.DataBind();16         }17         //点击Repeater1中的按钮时发生18         Repeater1.ItemCommand += Repeater1_ItemCommand;19     }20 21     void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)22     {23         if (e.CommandName == "Delete")24         {25             new gouwuData().Delete(Convert.ToInt32(e.CommandArgument));26 27                 Repeater1.DataSource = new gouwuData().Select();28                 Repeater1.DataBind();29         }30     }31 }
后台:

不用Repeater展示:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2  3  4  5  6  7     
8 9 79 80 81 82
83
84
85 86
87 88
89
90 91
92
93 94
界面
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class Default2 : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         if (!IsPostBack)13         {14             Literal1.Text = DataBind();15         }16     }17     public string DataBind()18     {19         string end = "";20         List
glist = new gouwuData().Select();21 foreach (gouwu g in glist)22 {23 if(g.name=="香蕉")24 {25 continue;26 }27 end += "
";28 end += "
";29 end += "
" + g.name + "
";30 end += "
价格:" + g.nowPrice + "
" + g.oldPrice + "
";31 end += "
" + g.context + "
";32 end += "
删除";33 end += "
";34 }35 36 return end;37 }38 }
后台
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class Delete : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         new gouwuData().Delete(Convert.ToInt32(Request["id"]));13         Response.Write("");14     }15 }
删除界面后台

 

转载于:https://www.cnblogs.com/maxin991025-/p/6261662.html

你可能感兴趣的文章
DPI , dot per inch
查看>>
怎样模拟日月地的运动关系
查看>>
浅谈如何做一名优秀的WEB前端工程师
查看>>
千个常用DOS命令全面收藏
查看>>
Rancher被Gartner评为“四大最酷云基础设施供应商”之一!
查看>>
Gradle学习笔记之Groovy
查看>>
yaf框架封装简单的pdo类
查看>>
在Centos上Rpm模式部署Mysql
查看>>
PHP中怎么使用PDO对象实现对MYsql数据库的增、删、改、查?
查看>>
安卓开源项目周报0426
查看>>
hexo博客解决不蒜子统计无法显示问题
查看>>
Hibernate中使用c3p0连接池
查看>>
sharepoint 工作流 infopath任务表单中 重复表数据的读写
查看>>
使用fio测试磁盘I/O性能报告
查看>>
python-20:爬取糗事百科段子源码
查看>>
通过qq通信原理
查看>>
U盘安装Windows和Ubuntu 15.04双系统图解教程
查看>>
我的友情链接
查看>>
免安装版tomcat启动startup.bat闪退问题
查看>>
Linux下架设CS1.6服务器
查看>>